package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"wwdmc/models/mysql"
	"wwdmc/models/redis"
	"wwdmc/pkg"

	"wwdmc/wwdmcrpc/internal/svc"
	"wwdmc/wwdmcrpc/wwdmcrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *wwdmcrpc.RegisterRequest) (*wwdmcrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	get := redis.Client.Get("sendsms" + in.Source + in.Tel)
	if get.Val() != in.Sendsms {
		return nil, fmt.Errorf("短信未验证,请重新输入")
	}
	register := mysql.Account{
		Model:    gorm.Model{},
		Tel:      in.Tel,
		Password: pkg.Md5(in.Password),
		Email:    in.Email,
	}
	mysql.DB.Debug().Model(&register).Create(&register)

	return &wwdmcrpc.RegisterResponse{Id: int64(register.ID)}, nil
}
